﻿using System.Collections.Generic;
using WMSSyncData.Model;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class Tddev105DAL : BaseSqlDBDAL<tddev105>
    {
        public List<tddev105> GetSyncData(string erpNo, string BillNo)
        {
            var sql = @"select max(t$pdsn)+1 as pdsn from ttddev105101 where t$pdno='{0}'";
            sql = string.Format(sql, erpNo);
            var dt = OracleDBHelper.GetDataSet(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                var pdsn = dt.Rows[0][0].ToString();
                if (pdsn == "")
                {
                    pdsn = "1";
                }
                sql = @"SELECT 
                        c.RelatedBill AS pdno,
                        0 AS pono,
                        {1} AS pdsn,
                        c.PartNumber AS item,
                        SUM(c.PutawayQty) AS qrdr,
                        0 AS qrdr1,
                          '1'AS host,
	                        ' ' AS mess,
	                        GETDATE() AS edte1,
	                        GETDATE() AS wdte1,
	                        0 AS refcntu,
	                        0 AS refcntd
                          FROM [dbo].[wms_T_InStock] os
                          LEFT JOIN [dbo].[wms_T_InStockRows] sr ON os.BillNo=sr.BillNo
                          LEFT JOIN dbo.wms_T_InStockRowDetails c ON os.BillNo=c.BillNo
                          WHERE os.Status='1' AND os.BillNo='{0}' AND c.RelatedBill='{2}'
                          GROUP BY os.Storage_SN,os.BillNo,c.RelatedBill,c.PartNumber ";
                sql = string.Format(sql, BillNo, pdsn, erpNo);
                dt = SqlDBHelper.GetDataSet(sql).Tables[0];
                var result = MakeTablePackage(dt);
                return result;
            }
            else
            {
                return new List<tddev105>();
            }
        }


    }
}
